﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VINETLIFE.BL;
using VINETLIFE.DO;
using System.Data;

namespace VINETLIFE.AdminCP
{
    public partial class ListImages : System.Web.UI.Page
    {
        public int CurrentPage
        {
            get
            {
                // look for current page in ViewState
                object o = this.ViewState["_CurrentPage"];
                if (o == null)
                    return 0;	// default to showing the first page
                else
                    return (int)o;
            }

            set
            {
                this.ViewState["_CurrentPage"] = value;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                Functions.ddlDatabinder(ddlProduct, cmsProductDO.PRODUCTID_FIELD, cmsProductDO.PRODUCTTITLE_FIELD, new cmsProductBL().SelectAll());
                dlImages.DataSource = new MediaBL().GetByType(true);
                dlImages.DataBind();
                BindList();
            }

        }



        protected void ddlProduct_SelectedIndexChanged(object sender, EventArgs e)
        {
            CurrentPage = 0;
            BindList();
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            Response.Redirect("/AdminCP/AddMultiImg.aspx");
        }


        protected void dlImages_SelectedIndexChanged(object sender, EventArgs e)
        {
            int ImagesID = int.Parse(dlImages.DataKeys[dlImages.SelectedIndex].ToString());
            Response.Redirect("/AdminCP/Images.aspx?ImagesID=" + ImagesID.ToString());
        }

        protected void dlImages_DeleteCommand(object source, DataListCommandEventArgs e)
        {
            int PLID = int.Parse(dlImages.DataKeys[e.Item.ItemIndex].ToString());
            new MediaBL().Delete(new MediaDO { MediaID = PLID });

            Functions.Alert("Xóa thành công", "Default.aspx?Page=ListImage");
        }

        private void binddatalist(DataTable dt)
        {
            // use PageDatasource object for paging  purpose..

            PagedDataSource Page = new PagedDataSource();
            try
            {


                Page.AllowPaging = true;

                //page object store the datasource of datalist..
                Page.DataSource = dt.DefaultView;
                Page.PageSize = 21;

                //"CurrentPage" is global variable that content the current page index..
                Page.CurrentPageIndex = CurrentPage;

                dlImages.DataSource = Page;
                dlImages.DataBind();
                //now datalist only show the a images... 
                //due to "Page.PageSize = 8;" statement...

                //visible true or false of next and previous button
                //according to last and first page...
                lbNext.Enabled = !Page.IsLastPage;
                lbPrev.Enabled = !Page.IsFirstPage;
            }

            catch (Exception ex)
            {
                throw ex;
            }

        }



        private void BindList()
        {
            int ProductID = int.Parse(ddlProduct.SelectedValue);
            if (ddlProduct.SelectedValue != "0")
                binddatalist(new MediaBL().SelectBySanPhamIDAdmin(ProductID, true));
            else
                binddatalist(new MediaBL().GetByType(true));
        }

        protected void lbNext_Click1(object sender, EventArgs e)
        {
            CurrentPage += 1;
            //"CurrentPage" is global variable that content the current page index..
            BindList();
        }

        protected void lbPrev_Click1(object sender, EventArgs e)
        {
            CurrentPage -= 1;
            //"CurrentPage" is global variable that content the current page index..
            BindList();
        }
    }
}